package com.yh.workbill.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yh.workbill.R;
import com.yh.workbill.model.WorkDevices;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class BottomDevicesSelectAdapter extends RecyclerView.Adapter<BottomDevicesSelectAdapter.ViewHolder> {

    private List<WorkDevices> list;
    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public BottomDevicesSelectAdapter(List<WorkDevices> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bottom_select, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WorkDevices workDevices = list.get(position);
        holder.tv_name.setText(workDevices.getName());
        if (position == list.size()) {
            holder.view.setVisibility(View.GONE);
        } else {
            holder.view.setVisibility(View.VISIBLE);
        }
        holder.itemView.setOnClickListener(v -> {
            if (onItemClick != null) {
                onItemClick.itemClick(workDevices);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        View view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            view = itemView.findViewById(R.id.view);
        }
    }

    public interface OnItemClick {
        void itemClick(WorkDevices workDevices);
    }
}
